#include <map>
#include <list>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <math.h>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;

#define forn(i,n) for(int i=0;i<n;i++)
#define fornr(i,n) for(int i=n-1;0<=i;i--)
#define forsn(i,s,n) for(int i=s;i<n;i++)
#define forsnr(i,s,n) for(int i=n-1;s<=i;i--)
#define forall(it,X) for(typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define forallr(it,X) for(typeof((X).rbegin()) it=(X).rbegin();it!=(X).rend();it++)

#define INF 2147483647

typedef pair<int,int> ii;
typedef list<int> li;
typedef list<ii> lii;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<double> vd;
typedef vector<vd> vvd;

typedef vector<vector<list<ii> > > multigraph;
typedef pair<int,pair<ii,ii> > state;

ii make_time(const string& s){
	ii res;
	stringstream ss(s,stringstream::in);
	char c; int p = 0;

	while( ss>>c && c!=':' ){
		res.first = 10*res.first + atoi(&c);
		p++;
	}

	ss>>c;
	res.second = 10*atoi(&c);
	ss>>c;
	res.second += atoi(&c);

	return res;
}

int main(){

	int N; cin >> N;
	forn(c,N){

		int T; cin >> T;
		map<string,int> Station;

		list<pair<int,ii> > routes[T];

		/* Por cada ruta */
		forn(r,T){

			int S; cin >> S;
//			cout << S << endl;
			/* por cada estacion */
			forn(s,S){

				string s1, s2;
				cin >> s1 >> s2;

				Station.insert(pair<string,int>(s2,Station.size()));
				routes[r].push_back(pair<int,ii>(Station[s2],make_time(s1)));

//				cout << s1 << " " << s2 << endl;
			}
		}

		string s; int src, dst;
		cin >> s;
//		cout << s << endl;
		src = Station[s];
		cin >> s;
		dst = Station[s];
//		cout << s << endl;
/*
		forall(it,Station) cout << it->first << " " << it->second << endl;
		forn(r,T){
			forall(it,routes[r]) cout << it->first << " " << it->second.first << " " << it->second.second << endl;
		}
*/
		int stations = Station.size();

		multigraph M(stations,vector<list<ii> >(stations));

		/* Armo el multigrafo con los datos */

		forn(r,T){
			int pre, pre_h, pre_m;
			forall(it,routes[r]){
				if(it->first!=r){
					int h = pre_h+it->second.first;
					int m = pre_m+it->second.second;
					h += m/60;
					m %= 60;
					M[r][it->first].push_back(ii(h,m));
//					cout << it->first << " " << it->second.first << " " << it->second.second << endl;
				}else{
					pre = it->first;
					pre_h = it->second.first;
					pre_m = it->second.second;
				}
			}
		}

		/* Algoritmo */
/*
		forall(it,M) forall(it2,*it){
			forall(it3,*it2) cout << it3->first << " " << it3->second << ", ";
			if(!(*it2).empty()) cout << endl;
		}
*/
		vint pushes(stations,-1);
		vint visitado(stations,0);
		queue<state > > q;
		q.push(pair(src,pair(0,0),pair(0,0))); // todos forall
		while(!q.empty()){
			state top = q.front();
			if( pushes[top.first]==-1 || visitado[top.first] <= pushes[top.first] ){

				// actualizo
				visitado[top.first]++;

				// push de los hijos
				forall(it,M[top.first]){
					forall(j,stations) if(!M[top.first][j].empty()){

						q.push( j , top.second.first , M[top.first][j] );

					}
				}

			}
		}
	}

	return 0;
}
